package com.spyx.user.service.impl;

import com.atyx.spyx.model.user.Leader;
import com.atyx.spyx.model.user.User;
import com.atyx.spyx.model.user.UserDelivery;
import com.atyx.spyx.vo.user.LeaderAddressVo;
import com.atyx.spyx.vo.user.UserLoginVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spyx.user.mapper.LeaderMapper;
import com.spyx.user.mapper.UserDeliveryMapper;
import com.spyx.user.mapper.UserMapper;
import com.spyx.user.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Override
    public User getUserByOpenId(String openid) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getOpenId,openid);
        User user = baseMapper.selectOne(wrapper);
        return user;
    }

    @Autowired
    private UserDeliveryMapper userDeliveryMapper;
    @Autowired
    private LeaderMapper leaderMapper;

    @Override
    public UserLoginVo getUserLoginVo(Long userid) {
        //1.查询用户的基本信息-user表中
        User user = baseMapper.selectById(userid);

        //2.仓库和团长信息
        UserDelivery userDelivery = userDeliveryMapper.selectOne(new LambdaQueryWrapper<UserDelivery>().eq(UserDelivery::getUserId, userid));

        //3.封装到userLoginVo中
        UserLoginVo userLoginVo = new UserLoginVo();
        BeanUtils.copyProperties(user,userLoginVo); // photoUrl,nickName,isNew,openId
        userLoginVo.setUserId(userid);
        if(userDelivery==null){ //没有根据用户id,查询出团长和仓库id
            userLoginVo.setLeaderId(1L);
            userLoginVo.setWareId(1L);
        }else{
            userLoginVo.setLeaderId(userDelivery.getLeaderId());
            userLoginVo.setWareId(userDelivery.getWareId());
        }
        return userLoginVo;
    }

    @Override
    public LeaderAddressVo getLeaderAddressByUserId(Long userId) {
        //1.查询user_delivery表,条件:userid,查询结果是:团长的id
        LambdaQueryWrapper<UserDelivery> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserDelivery::getUserId,userId);
        queryWrapper.eq(UserDelivery::getIsDefault,1);//默认的提货点

        UserDelivery userDelivery = userDeliveryMapper.selectOne(queryWrapper);
        if(userDelivery==null){
            return null;
        }

        //2.根据userDelivery表中团长id,查询 leader团长表
        Leader leader = leaderMapper.selectById(userDelivery.getLeaderId());

        //3.将leader的数据封装到LeaderAddressVo
        LeaderAddressVo leaderAddressVo = new LeaderAddressVo();
        BeanUtils.copyProperties(leader,leaderAddressVo);
        leaderAddressVo.setUserId(userId);
        leaderAddressVo.setLeaderId(leader.getId());
        leaderAddressVo.setLeaderName(leader.getName());
        leaderAddressVo.setLeaderPhone(leader.getPhone());
        leaderAddressVo.setWareId(userDelivery.getWareId());//仓库id
        leaderAddressVo.setStorePath(leader.getStorePath());

        return leaderAddressVo;
    }
}
